<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
    <!-- 引入element样式 -->
    <!-- 引入样式 -->
<!--    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/element-ui/2.13.1/theme-chalk/index.css">-->
    <link rel="stylesheet" type="text/css" href="../../static/css/index.css">
    <link rel="stylesheet" type="text/css" href="../../static/css/localIndex.css">
</head>
<body >
    <div id="app">
        <el-container class="container">
            <el-header class="indexHeader">
                <div class="block" style="float: left;">
                  <img src="../../static/image/logo.ico" width="60px" height="60px" style="border-radius: 50%;margin-left: 45px;"/>
                </div>
                <el-button type="primary" icon="el-icon-place" class="indexMeter" @click="backIndex">返回首页</el-button>
                <el-button type="primary" icon="el-icon-c-scale-to-original" class="indexMeter" @click="goMeter">添加仪表</el-button>
                <el-button type="primary" icon="el-icon-refresh" class="indexMeter" @click="restartSerialPort">重启串口</el-button>
                <template >
                    <el-select v-model="serialPort" filterable placeholder="请选择重启串口" class="indexMeter" >
                        <el-option
                                v-for="item in serialPorts"
                                :key="item"
                                :label="item"
                                :value="item">
                        </el-option>
                    </el-select>
                </template>
            </el-header>
                <el-container>
                    <el-aside width="210px">
                        <el-radio-group v-model="isCollapse" style="margin-bottom: 20px;">
                            <el-radio-button :label="false">功能选项</el-radio-button>
                            <!--<el-radio-button :label="true">选项收起</el-radio-button>-->
                        </el-radio-group>
                        <el-menu style="height: 700px" default-active="1-4-1" class="el-menu-vertical-demo" @open="handleOpen" @close="handleClose" :collapse="isCollapse">
                            <el-menu-item index="1">
                                <template slot="title">
                                    <i class="el-icon-s-order"></i>
                                    <span slot="title" @click="open('allCounter.html')">仪表监控</span>
                                </template>
                            </el-menu-item>
                            <el-menu-item index="2">
                                <template slot="title">
                                    <i class="el-icon-s-operation"></i>
                                    <span slot="title" @click="open('diagram.html')">曲线监控</span>
                                </template>
                            </el-menu-item>
                            <el-menu-item index="3">
                                <template slot="title">
                                    <i class="el-icon-s-data"></i>
                                    <span slot="title" @click="open('histogram.html')">柱状监控</span>
                                </template>
                            </el-menu-item>
                        </el-menu>
                    </el-aside>
                    <el-container >
                        <el-main class="right">
                            <iframe class="indexIframe" :src="iframeUrl"></iframe>
                        </el-main>
                    </el-container>
                </el-container>
            <div class="indexFooter">
                <p class="companyName">宜春市山腾电子有限公司</p>
            </div>
        </el-container>
    </div>
<!--    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>-->
<!--    <script src="https://cdn.bootcdn.net/ajax/libs/element-ui/2.13.2/index.js"></script>-->
<script src="../../static/js/vue.js"></script>
<script src="../../static/js/index.js"></script>
<script src="../../static/js/axios.min.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            isCollapse: false,
            loading: false,
            iframeUrl:"allCounter.html",
            dialog:false,
            form:{},
            timer: null,
            failCnt : 0,
            serialPorts: null,
            serialPort: '',
            src: 'https://cube.elemecdn.com/6/94/4d3ea53c084bad6931a56d5158a48jpeg.jpeg'
        },
        created:function(){
            var self = this;
            // 获取到所有的串口
            self.findAllPorts();
        },
        methods:{
            findAllPorts: function() {
                var self = this;
                axios.post('http://localhost:8032/device/findAllPorts').then(function (res) {
                    if (res.data.code == true) {
                        self.$message({
                            showClose: true,
                            message: '串口列表获取成功',
                            type: 'success'
                        });
                        self.serialPorts = res.data.data;
                        // self.serialPorts = res.data.data;
                        return;
                    } else {
                        if (self.failCnt < 30) {
                            self.failCnt = self.failCnt + 1;
                            setTimeout(self.findAllPorts, 1000);
                        } else {
                            self.$message({
                                showClose: true,
                                message: '串口列表获取失败',
                                type: 'error'
                            });
                            return ;
                        }

                    }
                }).catch(function () {
                    if (self.failCnt < 30) {
                        self.failCnt = self.failCnt + 1;
                        setTimeout(self.findAllPorts, 1000);
                    } else {
                        self.$message({
                            showClose: true,
                            message: '串口列表获取失败',
                            type: 'error'
                        });
                        return;
                    }
                })
            },
            // 重启串口
            restartSerialPort:function(){
                var self = this;
                axios.post('http://localhost:8032/device/restartSerialPort',{serialPort: this.serialPort}).then(function (res) {
                    if (res.data.code == true) {
                        self.$message({
                            showClose: true,
                            message: '串口重启成功',
                            type: 'success'
                        });
                        return;
                    } else {
                        self.$message({
                            showClose: true,
                            message: '串口重启失败',
                            type: 'error'
                        });
                        return ;
                    }
                }).catch(function () {
                    self.$message({
                        showClose: true,
                        message: '串口重启失败',
                        type: 'error'
                    });
                    return ;
                })
            },
            open:function(url){
                this.iframeUrl = url;
            },
            handleOpen: function(key, keyPath) {
                console.log(key, keyPath);
            },
            handleClose: function(key, keyPath) {
                console.log(key, keyPath);
            },
            goCommunication:function () {
                var self = this;
                self.iframeUrl =  "communicationChoice.html";
            },
            goMeter:function () {
                var self = this;
                self.iframeUrl =  "meter.html";
            },
            backIndex:function () {
                window.location.reload();
            }
        }
    })
</script>
</body>
</html>